<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({expectedData, actualData,rengong} = {}) {
      this.chart.setOption({
        xAxis: {
          data: this.chartData.map(item => item.date),
          boundaryGap: false,
          axisLabel:{
            fontSize: 15
          },
          axisTick: {
            show: false
          }
        },
        grid: {
          top: 60,
          left: 10,
          right: 30,
          bottom: 20,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          minInterval: 1,
          axisTick: {
            show: false
          },
          axisLabel:{
            fontSize: 15
          },
          splitArea: {
            show: false
          }
        },
        legend: {
          right: '30',
          top: '10',
          itemGap: 30,
          textStyle: {
            fontSize: 16
          }
        },
        series: [{
          name: '运行中上报',
          itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            }
          },
          smooth: false,
          type: 'line',
          data: this.chartData.map(item => item.type0),
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
          {
            name: '人工上报',
            smooth: false,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#3888fa',
                lineStyle: {
                  color: '#3888fa',
                  width: 2
                }
              }
            },
            data: this.chartData.map(item => item.type1),
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          },
          {
            name: '开机自检上报',
            smooth: false,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#fac858',
                lineStyle: {
                  color: '#fac858',
                  width: 2
                },
              }
            },
            data: this.chartData.map(item => item.type2),
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          }
        ]
      })
    }
  }
}
</script>
